package com.example.computer_exam_training_sys.controller;
import com.example.computer_exam_training_sys.entity.TopicCommentInfo;
import com.example.computer_exam_training_sys.service.TopicCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/topic-comment") // 修改前缀为 /topic-comment
@CrossOrigin
public class TopicCommentController {
    @Autowired
    private TopicCommentService commentService;

    @PostMapping("/add")
    public int addComment(@RequestBody TopicCommentInfo comment) {
        return commentService.addComment(comment);
    }

    @DeleteMapping("/delete")
    public int deleteComment(@RequestParam Integer commentId) {
        return commentService.deleteComment(commentId);
    }

    @GetMapping("/list")
    public Map<String, Object> getCommentsByTopicId(
            @RequestParam Integer topicId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return commentService.getCommentsByTopicId(topicId, page, size);
    }

    // 新增接口：获取多个帖子标题
    @PostMapping("/get-titles-by-id")
    public List<Map<String, Object>> getTopicTitlesById(@RequestBody List<Integer> topicId) {
        return commentService.getCommentsTitlesById(topicId);
    }

    // 获取用户的所有评论：GET http://localhost:8080/topic-comment/list-by-user
    @GetMapping("/list-by-user")
    public Map<String, Object> getCommentsByUserId(
            @RequestParam Integer userId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "5") Integer size) {
        return commentService.getCommentsByUserId(userId, page, size);
    }
}